package com.juyu.miliao.common.netease;

import com.juyu.miliao.common.domain.NetEaseConfig;
import com.juyu.miliao.common.util.CheckSumBuilder;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpPost;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class DecorateHttpHeader {

    /**
     * 装饰请求头
     *
     * @param httpPost
     * @return
     */
    public static HttpPost getDecorateHttpHeader(HttpPost httpPost) {
        // 计算请求头必填参数
        String nonce = (int) ((Math.random() * 9 + 1) * 10000) + "";
        String curTime = String.valueOf((new Date()).getTime() / 1000L);
        String checkSum = CheckSumBuilder.getCheckSum(NetEaseConfig.getAppSecret(), nonce, curTime);//计算CheckSum的java代码
        // 设置请求的header
        httpPost.addHeader("AppKey", NetEaseConfig.getAppKey());
        httpPost.addHeader("Nonce", nonce);
        httpPost.addHeader("CurTime", curTime);
        httpPost.addHeader("CheckSum", checkSum);
        httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
        return httpPost;
    }

    /**
     * 装饰请求头
     *
     * @param httpPost
     * @return
     */
    public static HttpDelete getDecorateHttpHeaderToHttpDelete(HttpDelete httpDelete) {
        // 计算请求头必填参数
        String nonce = (int) ((Math.random() * 9 + 1) * 10000) + "";
        String curTime = String.valueOf((new Date()).getTime() / 1000L);
        String checkSum = CheckSumBuilder.getCheckSum(NetEaseConfig.getAppSecret(), nonce, curTime);//计算CheckSum的java代码
        // 设置请求的header
        httpDelete.addHeader("AppKey", NetEaseConfig.getAppKey());
        httpDelete.addHeader("Nonce", nonce);
        httpDelete.addHeader("CurTime", curTime);
        httpDelete.addHeader("CheckSum", checkSum);
        httpDelete.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
        return httpDelete;
    }

}
